$(function(){
    // ym.loadFun();
	/*获取系统课程*/
	var informHTML="";
	var remindHTML="";
	var activeHTML="";
    var stateHTML="<span style='display:block; width:8px;height:8px;border-radius:50%;background-color:#69e600;'></span>";
	function getInform(){
		ym.ajax({
        url: "/ymUserInfrom/information",
        param:{
           /* userId:'1'*/
        },
        success: function (data) {
        	var newData=data.obj;
        	console.log(newData);
        	if(newData.length!==0){
        		$('section').html("");
        		for (var i = 0; i < newData.length; i++){
        			informHTML="";
        			if(newData[i].state==0){
                     	var time = newData[i].informTime.substr('0','10');
                     	var zhi = newData[i].informText.substr('0','20');
                     	zhi = zhi+"...";
                     	informHTML+='<dl class="am-accordion-item infor" curIndex="'+newData[i].inform_id+'">\n'
				       	informHTML+='<dt class="am-accordion-title"><span class="dian"></span>'+zhi+'</dt>\n'
	                   	informHTML+='<dd class="am-accordion-bd am-collapse">\n'
	                   	informHTML+='<div class="am-accordion-content">\n'+newData[i].informText+'<p class="am-text-right">'+time+'</p></div>\n'
	                   	informHTML+='</dd>\n'
	                   	informHTML+='</dl>'
	                   	$('section').append(informHTML);
				    }else{
				    	var time = newData[i].informTime.substr('0','10');
                     	var zhi = newData[i].informText.substr('0','20');
                     	zhi = zhi+"...";
                       informHTML+='<dl class="am-accordion-item infor" curIndex="'+newData[i].inform_id+'"">\n'
				       informHTML+='<dt class="am-accordion-title">'+zhi+'</dt>\n'
	                   informHTML+='<dd class="am-accordion-bd am-collapse">\n'
	                   informHTML+='<div class="am-accordion-content">\n'+newData[i].informText+'<p class="am-text-right">'+time+'</p></div>\n'
	                   informHTML+='</dd>\n'
	                   informHTML+='</dl>'
	                   $('section').append(informHTML);
                    }
                }
        		
        	}else{
        		informHTML = '<li class="am-text-center"><img style="height:150px" src="../img/no.jpg"></li>';
        		$('section').html(informHTML);
        	}
        }
    	});
	}
	$('body').on('click','dt',function(){
   		$(this).next().slideToggle(500);
   	})
	function getRemind(){
		ym.ajax({
        url: "/ymUserInfrom/remind",
        param:{
           /* userId:'1'*/
        },
        success: function (data) {
        	var newData=data.obj;
        	if(newData.length!==0){
        		for (var i = 0; i < newData.length; i++) {
                    remindHTML="";
                    if(newData[i].state==1){
                        remindHTML+="<li curIndex='"+newData[i].inform_id+"'>"+
                                    "<div class='mess-tab'>"+
                                    "<div>"+
                                    "<p class='head-img'><img src='"+newData[i].child_headimgurl+"' /></p>"+
                                    "<p>"+newData[i].childName+"</p>"+
                                    "</div>"+
                                    "<div class='inform-desc'>"+
                                    "<p>"+newData[i].informText+"</p>"+
                                    "<p>"+newData[i].informTime.split('T')[0]+" "+newData[i].informTime.split('T')[1].split('.')[0]+"</p>"+
                                    "</div>"+
                                    "</div>"+
                                    "</li>";
                    }else{
                        remindHTML+="<li curIndex='"+newData[i].inform_id+"'>"+
                                    "<div class='mess-tab'>"+stateHTML+
                                    "<div>"+
                                    "<p class='head-img'><img src='"+newData[i].child_headimgurl+"' /></p>"+
                                    "<p>"+newData[i].childName+"</p>"+
                                    "</div>"+
                                    "<div class='inform-desc'>"+
                                    "<p>"+newData[i].informText+"</p>"+
                                    "<p>"+newData[i].informTime.split('T')[0]+" "+newData[i].informTime.split('T')[1].split('.')[0]+"</p>"+
                                    "</div>"+
                                    "</div>"+
                                    "</li>";
                    }
        			
        		}
        		$('.reminds').html(remindHTML);
        	}else{
        		remindHTML = '<li class="am-text-center"><img style="height:150px" src="../img/no.jpg"></li>';
        		$('.reminds').html(remindHTML);
        	}
        	
        }
    	});
	}
	/*获取互动消息*/
	function getActive(){
        activeHTML="";
		ym.ajax({
        url: "/ymUserInfrom/interactive",
        param:{
           /* userId:'1'*/
        },
        success: function (data) {
        	console.log(data);
        	var newData=data.obj;
                if(newData){
        			/*list1 交流圈*/
        			if(newData.list1.length!==0){
        				 activeHTML+="<div class='title-tab'><h3>来自交流圈：</h3></div>";
        				for (var j = 0; j < newData.list1.length; j++) {
                            if(newData.list1[j].read_state==1){
                            	var tiezi = newData.list1[j].ecchange_message.substr('0','15');
                            	tiezi = tiezi+"...";
                            	activeHTML+="<li curIndex='"+newData.list1[j].type_id+"' curType='"+newData.list1[j].type+"' answerId='"+newData.list1[j].answer_id+"' userId='"+newData.list1[j].userId+"'>"+
                                            "<div class='mess-tab'>"+
                                            "<div>"+
                                            "<p class='head-img'><img src='"+newData.list1[j].headimgurl+"' /></p>"+
                                            "</div>"+
                                            "<div class='inform-desc'>"+
                                            "<h4><span class='personName'>"+newData.list1[j].nickname+"</span></h4>"+
                                            "<p class='tis'>"+newData.list1[j].createTime+"</p>"+
                                            "<p class='nrit'>"+newData.list1[j].answer_des+"</p>"+
                                            "<p class='nrit'><span class='color69e600'>来自于帖子：</span>"+tiezi+"</p>"+
                                            "</div>"+
                                            "</div>"+
                                            "</li>";
                            }else{
                            	var tiezi = newData.list1[j].ecchange_message.substr('0','15');
                            	tiezi = tiezi+"...";
                                activeHTML+="<li curIndex='"+newData.list1[j].exchangeId+"' curType='"+newData.list1[j].type+"' answerId='"+newData.list1[j].answer_id+"' userId='"+newData.list1[j].userId+"'>"+
                                            "<div class='mess-tab'>"+stateHTML+
                                            "<div>"+
                                            "<p class='head-img'><img src='"+newData.list1[j].headimgurl+"' /></p>"+
                                            "</div>"+
                                            "<div class='inform-desc'>"+
                                            "<h4><span class='personName'>"+newData.list1[j].nickname+"</span></h4>"+
                                            "<p class='tis'>"+newData.list1[j].createTime+"</p>"+
                                            "<p class='nrit'>"+newData.list1[j].answer_des+"</p>"+
                                            "<p class='nrit'><span class='color69e600'>来自于帖子：</span>"+tiezi+"</p>"+
                                            "</div>"+
                                            "</div>"+
                                            "</li>";
                            }
        					
	        			}
        			}
        			/*list2 文章回复*/
        			if(newData.list2.length!==0){
        				activeHTML+="<div class='title-tab'><h3>来自文章：</h3></div>";
        				for (var k = 0; k < newData.list2.length; k++) {
                            if(newData.list2[k].read_state==1){
                            	var tiezi = newData.list2[k].answer_des1.substr('0','15');
                            	tiezi = tiezi+"...";
                                activeHTML+="<li curType='"+newData.list2[k].type+"'  articleId='"+newData.list2[k].type_id+"' answerId='"+newData.list2[k].answer_id+"'>"+
                                            "<div class='mess-tab'>"+
                                            "<div>"+
                                            "<p class='head-img'><img src='"+newData.list2[k].headimgurl+"' /></p>"+
                                            "</div>"+
                                            "<div class='inform-desc'>"+
                                            "<h4><span class='personName'>"+newData.list2[k].nickname+"</span></h4>"+
                                            "<p class='tis'>"+newData.list2[k].createTime+"</p>"+
                                            "<p class='nrit'>"+newData.list2[k].answer_des+"</p>"+
                                             "<p class='nrit'><span class='color69e600'>来自于我的评论：</span>"+tiezi+"</p>"+
                                            "</div>"+
                                            "</div>"+
                                            "</li>";
                            }else{
                            	var tiezi = newData.list2[k].answer_des1.substr('0','15');
                            	tiezi = tiezi+"...";
                                activeHTML+="<li curType='"+newData.list2[k].type+"'  articleId='"+newData.list2[k].type_id+"' answerId='"+newData.list2[k].answer_id+"'>"+
                                            "<div class='mess-tab'>"+stateHTML+
                                            "<div>"+
                                            "<p class='head-img'><img src='"+newData.list2[k].headimgurl+"' /></p>"+
                                            "</div>"+
                                            "<div class='inform-desc'>"+
                                            "<h4><span class='personName'>"+newData.list2[k].nickname+"</span></h4>"+
                                            "<p class='tis'>"+newData.list2[k].createTime+"</p>"+
                                            "<p class='nrit'>"+newData.list2[k].answer_des+"</p>"+
                                             "<p class='nrit'><span class='color69e600'>来自于我的评论：</span>"+tiezi+"</p>"+
                                            "</div>"+
                                            "</div>"+
                                            "</li>";
                            }
        					
	        			}
        			}
                    
        			/*list3 问医生*/
        			if(newData.list3.length!==0||newData.list5.length){
        				activeHTML+="<div class='title-tab'><h3>来自问医生：</h3></div>";
        				for (var i = 0; i < newData.list3.length; i++) {
                             if(newData.list3[i].read_state==1){
                             	var tiezi = newData.list3[i].answer_des1.substr('0','15');
                            	tiezi = tiezi+"...";
                                activeHTML+="<li curType='"+newData.list3[i].type+"' answerId='"+newData.list3[i].answer_id+"' curTypeId='"+newData.list3[i].typeId+"' docId='"+newData.list3[i].answerUserId+"'  parentId='"+newData.list3[i].parentId+"'>"+
                                            "<div class='mess-tab'>"+
                                            "<div>"+
                                            "<p class='head-img'><img src='"+newData.list3[i].headimgurl+"' /></p>"+
                                            "</div>"+
                                            "<div class='inform-desc'>"+
                                            "<h4><span class='personName'>"+newData.list3[i].doctorRealName+"</span></h4>"+
                                            "<p class='tis'>"+newData.list3[i].createTime+"</p>"+
                                            "<p class='nrit'>"+newData.list3[i].answer_des+"</p>"+
                                            "<p class='nrit'><span class='color69e600'>来自于医生的回复：</span>"+tiezi+"</p>"+
                                            "</div>"+
                                            "</li>";
                            }else{
                            	var tiezi = newData.list3[i].answer_des1.substr('0','15');
                            	tiezi = tiezi+"...";
                                activeHTML+="<li curType='"+newData.list3[i].type+"' answerId='"+newData.list3[i].answer_id+"' curTypeId='"+newData.list3[i].typeId+"' docId='"+newData.list3[i].answerUserId+"'  parentId='"+newData.list3[i].parentId+"'>"+
                                            "<div class='mess-tab'>"+stateHTML+
                                            "<div>"+
                                            "<p class='head-img'><img src='"+newData.list3[i].headimgurl+"' /></p>"+
                                            "</div>"+
                                            "<div class='inform-desc'>"+
                                            "<h4><span class='personName'>"+newData.list3[i].doctorRealName+"</span></h4>"+
                                            "<p class='tis'>"+newData.list3[i].createTime+"</p>"+
                                            "<p class='nrit'>"+newData.list3[i].answer_des+"</p>"+
                                             "<p class='nrit'><span class='color69e600'>来自于医生的回复：</span>"+tiezi+"</p>"+
                                            "</div>"+
                                            "</li>";
                            }
        					
	        			}
        				/*list5问医生*/
                        for (var m = 0; m < newData.list5.length; m++) {
                             if(newData.list5[m].read_state==1){
                             	var tiezi = newData.list5[m].answer_des1.substr('0','15');
                            	tiezi = tiezi+"...";
                                activeHTML+="<li curType='"+newData.list5[m].type+"' answerId='"+newData.list5[m].answer_id+"' curTypeId='"+newData.list5[m].typeId+"' docId='"+newData.list5[m].answerUserId+"'  parentId='"+newData.list5[m].parentId+"'>"+
                                            "<div class='mess-tab'>"+
                                            "<div>"+
                                            "<p class='head-img'><img src='"+newData.list5[m].headimgurl+"' /></p>"+
                                            "</div>"+
                                            "<div class='inform-desc'>"+
                                            "<h4><span class='personName'>"+newData.list5[m].nickname+"</span></h4>"+
                                            "<p class='tis'>"+newData.list5[m].createTime+"</p>"+
                                            "<p class='nrit'>"+newData.list5[m].answer_des+"</p>"+
                                            "<p class='nrit'><span class='color69e600'>来自用户的回复：</span>"+tiezi+"</p>"+
                                           "</div>"+
                                            "</li>";
                            }else{
                            	var tiezi = newData.list5[m].answer_des1.substr('0','15');
                            	tiezi = tiezi+"...";
                                activeHTML+="<li curType='"+newData.list5[m].type+"' answerId='"+newData.list5[m].answer_id+"' curTypeId='"+newData.list5[m].typeId+"' docId='"+newData.list5[m].answerUserId+"'  parentId='"+newData.list5[m].parentId+"'>"+
                                            "<div class='mess-tab'>"+stateHTML+
                                            "<div>"+
                                            "<p class='head-img'><img src='"+newData.list5[m].headimgurl+"' /></p>"+
                                            "</div>"+
                                            "<div class='inform-desc'>"+
                                            "<h4><span class='personName'>"+newData.list5[m].nickname+"</span></h4>"+
                                            "<p class='tis'>"+newData.list5[m].createTime+"</p>"+
                                            "<p class='nrit'>"+newData.list5[m].answer_des+"</p>"+
                                            "<p class='nrit'><span class='color69e600'>来自用户的回复：</span>"+tiezi+"</p>"+
                                            "</div>"+
                                            "</li>";
                            }
                            
                        }
        			}
                   
                    /*list4 来自我的帖子的回复*/
                    if(newData.list4.length!==0){
                        activeHTML+="<div class='title-tab'><h3>来自我的帖子的回复：</h3></div>";
                        for (var i = 0; i < newData.list4.length; i++) {
                             if(newData.list4[i].read_state==1){
                             	var tiezi = newData.list4[i].ecchange_message.substr('0','15');
                            	tiezi = tiezi+"...";
                                activeHTML+="<li curIndex='"+newData.list4[i].exchangeId+"' curType='"+newData.list4[i].type+"' answerId='"+newData.list4[i].answer_id+"' userId='"+newData.list4[i].userId+"'>"+
                                            "<div class='mess-tab'>"+
                                            "<div>"+
                                            "<p class='head-img'><img src='"+newData.list4[i].headimgurl+"' /></p>"+
                                            "</div>"+
                                            "<div class='inform-desc'>"+
                                            "<h4><span class='personName'>"+newData.list4[i].nickname+"</span></h4>"+
                                            "<p class='tis'>"+newData.list4[i].createTime+"</p>"+
                                            "<p class='nrit'>"+newData.list4[i].answer_des+"</p>"+
                                            "<p class='nrit'><span class='color69e600'>来自于帖子：</span>"+tiezi+"</p>"+
                                            "</div>"+
                                            "</li>";
                            }else{
                            	var tiezi = newData.list4[i].ecchange_message.substr('0','15');
                            	tiezi = tiezi+"...";
                                activeHTML+="<li curIndex='"+newData.list4[i].exchangeId+"' curType='"+newData.list4[i].type+"' answerId='"+newData.list4[i].answer_id+"' userId='"+newData.list4[i].userId+"'>"+
                                            "<div class='mess-tab'>"+stateHTML+
                                            "<div>"+
                                            "<p class='head-img'><img src='"+newData.list4[i].headimgurl+"' /></p>"+
                                            "</div>"+
                                            "<div class='inform-desc'>"+
                                            "<h4><span class='personName'>"+newData.list4[i].nickname+"</span></h4>"+
                                            "<p class='tis'>"+newData.list4[i].createTime+"</p>"+
                                            "<p class='nrit'>"+newData.list4[i].answer_des+"</p>"+
                                            "<p class='nrit'><span class='color69e600'>来自于帖子：</span>"+tiezi+"</p>"+
                                            "</div>"+
                                            "</li>";
                            }
                            
                        }
                    }
                    else{
                        activeHTML="";
                    }
        		$('.active-mess').html(activeHTML);
        	}else{
               $('.active-mess').html('<li class="am-text-center" style="padding:1rem"><img style="height:150px" src="../img/no.jpg"></li>');
            }
        }
    	});
	}
	getInform();
	getRemind();
	getActive();
    /*改变互动消息的阅读状态*/
 	/*查看消息详情*/
            $('body').on('click','.active-mess li',function(){
 			var curIndex=$(this).attr('curIndex');
 			var curType=$(this).attr('curType');
 			var curTypeId;
 			var answerUserId;
            var curIndex1=$(this).attr('answerId');
            ym.ajax({
                url: "/ymUserInfrom/readState",
                param:{
                   answerId:curIndex1
                },
                success: function (data) {
                    getActive();
                }
            });
 			if(curType=='3'){
                /*交流圈详情*/
                        window.location.href=''
                        typeId=$(this).attr('curIndex');
                        var userId = $(this).attr('userId');
                       window.location.href="topic.html?exchangeId="+typeId+"&userid="+userId;
                    }else if(curType=='1'){
                        /*文章*/
                        typeId=$(this).attr('articleId');
                        var id = $(this).attr('id');
                        ym.ajax({
                            url:'/article/updateArticleReadNumber',
                            type:'post',
                            param:{ articleId:typeId},
                            success:function(data){
                                if(data.res ==1){
                                    console.log("数据获取成功跳转页面！")
                                }else{console.log("数据获取失败！")}

                                window.location.href='yimiaoxinwenXq.html?articleId='+typeId;
                            }
                        });
                    }else if(curType=='2'){
                        /*问医生*/
                        answerUserId=$(this).attr('parentId');
                        curTypeId=$(this).attr('curTypeId');
                        console.log(curTypeId)
                        ym.ajax({
                                url: "/Answer/queryYmAnswerListDetail",
                                param:{
                                    typeId:curTypeId,
                                    answerId:$(this).attr('answerId'),
                                    praiseType:4,
                                    parentAnswerId:$(this).attr('answerId')
                                },
                                success: function (data) {
                                    sessionStorage.setItem('articleXq',JSON.stringify(data.obj));       
                                    window.location.href="problemXq.html";
                                }
                            });
                    }
 			
 		})
        /*改变系统和提示消息的阅读状态*/
         $('body').on('click','.infor',function(){
            var curIndex=$(this).attr('curIndex');
            ym.ajax({
                url: "/ymUserInfrom/State",
                param:{
                   informId:curIndex
                },
                success: function (data) {
                	console.log(data);
                    getRemind();
                   /* getInform();*/
                }
            });
        })
})